package com.ruoyi.student.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.student.domain.DTO.TTaskInfoDTO;
import com.ruoyi.student.domain.TCourseInfo;
import com.ruoyi.student.domain.TUnitInfo;
import com.ruoyi.student.domain.vo.TTaskInfoVo;
import com.ruoyi.student.service.ITCourseInfoService;
import com.ruoyi.student.service.ITUnitInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.student.domain.TTaskInfo;
import com.ruoyi.student.service.ITTaskInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 任务Controller
 * 
 * @author ruoyi
 * @date 2023-08-15
 */
@RestController
@RequestMapping("/student/task")
public class TTaskInfoController extends BaseController
{
    @Autowired
    private ITTaskInfoService tTaskInfoService;
    @Autowired
    private ITCourseInfoService itCourseInfoService;
    @Autowired
    private ITUnitInfoService itUnitInfoService;

    /**
     * 查询任务列表
     */
    @PreAuthorize("@ss.hasPermi('student:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(TTaskInfo tTaskInfo)
    {
        startPage();
//        List<TTaskInfo> list = tTaskInfoService.selectTTaskInfoList(tTaskInfo);
        List<TTaskInfoVo> list = tTaskInfoService.selectTTaskInfoVoList(tTaskInfo);
        return getDataTable(list);
    }
    /**
     * 查询指定学生的任务列表
     */
    @PreAuthorize("@ss.hasPermi('student:task:recordList')")
    @GetMapping("/recordList")
    public TableDataInfo selectTTaskInfoVoAndRecordList(TTaskInfoDTO tTaskInfoDTO)
    {
        startPage();
//        List<TTaskInfo> list = tTaskInfoService.selectTTaskInfoList(tTaskInfo);
        List<TTaskInfoVo> list = tTaskInfoService.selectTTaskInfoVoAndRecordListByUnitId(tTaskInfoDTO.getUnitId(), tTaskInfoDTO.getStudentId());
        return getDataTable(list);
    }

    /**
     * 导出任务列表
     */
    @PreAuthorize("@ss.hasPermi('student:task:export')")
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TTaskInfo tTaskInfo)
    {
        List<TTaskInfo> list = tTaskInfoService.selectTTaskInfoList(tTaskInfo);
        ExcelUtil<TTaskInfo> util = new ExcelUtil<TTaskInfo>(TTaskInfo.class);
        util.exportExcel(response, list, "任务数据");
    }

    /**
     * 获取任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('student:task:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tTaskInfoService.selectTTaskInfoById(id));
    }

    /**
     * 新增任务
     */
    @PreAuthorize("@ss.hasPermi('student:task:add')")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TTaskInfo tTaskInfo)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        tTaskInfo.setCreatorId(user.getUserId());
        TCourseInfo tCourseInfo = itCourseInfoService.selectTCourseInfoById(Long.valueOf(tTaskInfo.getCourseId()));
        TUnitInfo tUnitInfo = itUnitInfoService.selectTUnitInfoById(Long.valueOf(tTaskInfo.getUnitId()));
        tTaskInfo.setCourseName(tCourseInfo.getName());
        tTaskInfo.setUnitName(tUnitInfo.getName());
        return toAjax(tTaskInfoService.insertTTaskInfo(tTaskInfo));
    }

    /**
     * 修改任务
     */
    @PreAuthorize("@ss.hasPermi('student:task:edit')")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TTaskInfo tTaskInfo)
    {
        return toAjax(tTaskInfoService.updateTTaskInfo(tTaskInfo));
    }

    /**
     * 删除任务
     */
    @PreAuthorize("@ss.hasPermi('student:task:remove')")
    @Log(title = "任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tTaskInfoService.deleteTTaskInfoByIds(ids));
    }
}
